#!/usr/bin/python2.5
"""
   fasta_translate.py

"""

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   options        


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

from optparse import OptionParser
import sys, os
import os.path
import StringIO

# add self to search path for testing
if __name__ == '__main__':
    exe_path = os.path.split(os.path.abspath(sys.argv[0]))[0]
    sys.path.append(os.path.abspath(os.path.join(exe_path,"..", "python_modules")))
    myname = os.path.split(sys.argv[0])[1]
    myname = os.path.splitext(myname)[0];
else:
    myname = __name__

from general_util import die_error
from custom_log import custom_log



parser = OptionParser(version="%prog 1.0")
parser.add_option("-v", "--verbose", dest = "verbose",
                  action="store_true", default=False,
                  help="Do not echo to shell but only print to log.")
parser.add_option("-f", "--frame", dest="frame",
                  metavar="FRAME", 
                  type="int",
                  help="Translation frame. 1,2,3 for 5'; -1,-2,-3 for 3'")
parser.add_option("--log_parameters", dest="log_parameters",
                    action="store_true", default=False,
                    help="Print program parameters as first line.")



parser.set_defaults(frame = 1)


# get help string
f =StringIO.StringIO()
parser.print_help(f)
helpstr = f.getvalue()
(options, remaining_args) = parser.parse_args()

if options.frame < -3 or options.frame > 3 or options.frame == 0:
    die_error("You have set FRAME == %d.\n" % options.frame + 
                "Please specify a FRAME which is between -3 and 3.\n\n"  + helpstr)


def save_option_parameters ():
    """
    Make a record of the program parameters so we can rerun things if necessary
    """
    programme_option_parameters = [myname + ".py"]
    for option, value in options.__dict__.iteritems():
        programme_option_parameters.append("--" + option + " " + str(value))

    programme_option_parameters = " ".join(programme_option_parameters)
    sys.stdout.write(programme_option_parameters + "\n")



#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   imports        


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

import StringIO
import re
import operator

import general_util
from general_util import die_error
from translate_by_codon import translate
from fasta_sequences_iterator import fasta_sequences_iterator
from translate_by_codon  import translate
from reverse_complement_dna import reverse_complement

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Functions        


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Main logic


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

if options.log_parameters:
    save_option_parameters ()






for seq_id, seq in fasta_sequences_iterator(sys.stdin):
    if options.frame < 0:
        seq = reverse_complement(seq)
    seq = (abs(options.frame) - 1) * "-" + seq
    sys.stdout.write(">" + seq_id + "\n" + translate(seq) + "\n")

